package com.p2p.user.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.p2p.common.base.BaseController;
import com.p2p.common.constants.Constants;
import com.p2p.common.constants.Message;
import com.p2p.common.enums.ResponseEnum;
import com.p2p.common.token.TokenService;
import com.p2p.user.po.User;
import com.p2p.user.service.PublicService;
import com.p2p.user.vo.RegisterVo;

/**
 * Created by Allen on 2017/3/19.
 */
@RestController
@RequestMapping(value = "public")
public class PublicController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(PublicController.class);
    @Autowired
    private PublicService publicService;
    @Resource
    private TokenService tokenService;
    /**
     * 注册接口
     *
     * @return
     */
    @RequestMapping(value = "register", method = RequestMethod.POST)
    public Message register(RegisterVo registerVo) {
        Message message = publicService.register(registerVo);
        return message;
    }

    /**
     * 登陆接口
     *
     * @return
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public Message login(HttpServletRequest request) {
        logger.info("用户调用了登录接口");
        Message message = publicService.login(request);
        return message;
    }
    
    /**
     * 发送邮件
     *
     * @return
     */
    @RequestMapping(value = "sendEmail", method = RequestMethod.GET)
    public Message sendEmail(HttpServletRequest request) {
    
        Message message = publicService.sendEmail(request);
        return message;
    }

    
    /**
     * 验证email
     *
     * @return
     */
    @RequestMapping(value = "checkEmail", method = RequestMethod.GET)
    public Message checkEmail(HttpServletRequest request) {
        Message message = publicService.checkEmail(request);
        return message;
    }
  

    /**
     * 上传图片
     *
     * @return
     */
    @RequestMapping(value = "uploadFile", method = RequestMethod.POST, produces = "application/json;charset=utf8")
    public Message uploadFile(HttpServletRequest request) throws IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartRequest.getFile("img");
        String originName = multipartFile.getOriginalFilename();// 取得原文件名
        String fileExt = originName.substring(originName.lastIndexOf(".") + 1);// 取得文件后缀
        SimpleDateFormat sd = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        Random random = new Random();
        int i = random.nextInt(999) % (999 - 100 + 1) + 100;
        String newName = sd.format(new java.util.Date()) + i + "." + fileExt;
        String path = ("/opt/upload/images");
        String file_url = Constants.server + "/upload/images/" + newName;
        //这里不必处理IO流关闭的问题，因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉，我是看它的源码才知道的
        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), new File(path, newName));
        return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), file_url);


    }
}
